package com.tszs.Controller;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import com.tszs.biz.CategoryManagementBiz;
import com.tszs.entity.CategoryManagement;
import com.tszs.entity.CategoryManagementExample;
import com.tszs.util.CallBack;
import com.tszs.util.Uuid;
import com.tszs.util.projectPath;

import net.sf.json.JSONObject;

/**
 * @类名:CategoryManagementController
 * @类描述：分类管理 @创建时间：2019-03-22 13:07:07
 * @创建人：王俊
 * @author Administrator
 *
 */
@Controller
public class CategoryManagementController {
	/**
	 * CategoryManagementBiz
	 * 
	 * @描述:分类管理
	 * @创建时间:201903-21 13:14:14
	 */
	@Autowired
	private CategoryManagementBiz categoryManagementBiz;

	/**
	 * @方法名:classifiedQuery
	 * @方法描述：分类管理查询 @创建时间：2019-03-22 13:07:07
	 * @创建人：王俊
	 * @author Administrator
	 *
	 */
	@RequestMapping("pc/admin/classifiedQuery")
	public void classifiedQuery(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		int page = Integer.parseInt(request.getParameter("page"));
		int limit = Integer.parseInt(request.getParameter("limit"));
		CategoryManagement example = new CategoryManagement();
		
		example.setPage((page - 1) * limit);
		example.setLimit(limit);
		example.setcType("1");
		
		List<CategoryManagement> selectByExample = categoryManagementBiz.selectByExamplePage(example);
		
		
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", selectByExample.size());
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		for (int i = 0; i < selectByExample.size(); i++) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("id", i + 1);
			mapData.put("getName", selectByExample.get(i).getcName());
			mapData.put("CreateTime",
					"<a target=\"_Blank\" href='" + projectPath.url +  selectByExample.get(i).getcPhoto()
							+ "'><img class='reviewImg' src='" + projectPath.url + 
							 selectByExample.get(i).getcPhoto() + "'></a>");
			String button = "<a type='button' class=\"layui-btn layui-btn-xs\" href=\"javascript:table('0','"
					+ selectByExample.get(i).getcId() + "','" + selectByExample.get(i).getcName()
					+ "')\"'>修改</a>";
			if(i>3) {
				button += "<a type='button' class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\"  href=\"javascript:table('1','"
					+ selectByExample.get(i).getcId() + "','" + selectByExample.get(i).getcName() + "')\">删除</a>";
			}
			mapData.put("button", button);
			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}

	/**
	 * @方法名称：UploadServlet
	 * @方法描述：类别管理文件上传 @创建时间：2019-03-23 13:07:07
	 * @创建人：王俊
	 * @param file
	 * @param request
	 * @param response
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping("pc/admin/UploadServlet")
	@ResponseBody
	public void UploadServlet(@RequestParam("file") MultipartFile file, HttpServletRequest request,
			HttpServletResponse response) throws IllegalStateException, IOException {
/*		String realPath = request.getSession().getServletContext().getRealPath("/images//");
		System.err.println(realPath);
		File filepath = new File(realPath);
		if (!filepath.exists()) // 判断文件夹是否存在
			filepath.mkdirs();// 创建此抽象路径名指定的目录，包括所有必需但不存在的父目录。
		// 文件保存路径
		String fileName = file.getOriginalFilename();
		String extensionname = fileName.substring(fileName.lastIndexOf(".") + 1);
		String fileNames = Uuid.getUUID();
		String newFilename = fileNames + "." + extensionname;
		String savePath = realPath + "\\" + newFilename;
		file.transferTo(new File(savePath));
		System.err.println(savePath);
		Map<String, Object> map2 = new HashMap<>();
		map2.put("src", newFilename);
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", map2);*/
		String fileName = file.getOriginalFilename();
		String extensionname = fileName.substring(fileName.lastIndexOf(".") + 1);
		String fileNames = Uuid.getUUID();
		String newFilename = "D:\\txrImages\\" +fileNames + "." + extensionname;
		file.transferTo(new File(newFilename));
		Map<String, Object> map2 = new HashMap<>();
		map2.put("src", fileNames + "." + extensionname);
		Map<String, Object> map = new HashMap<>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("data", map2);
		new CallBack().yes(response, map, "");
	}

	/**
	 * @方法名称：inserCateMange
	 * @方法描述：类别管理添加 @创建时间：2019-03-23 13:07:07
	 * @创建人：王俊
	 * @param file
	 * @param request
	 * @param response
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping("pc/admin/inserCateMange")
	public void inserCateMange(HttpServletRequest request, HttpServletResponse response) {
		try {
			CategoryManagement record = new CategoryManagement();
			record.setcId(Uuid.getUUID());
			record.setcCreateTime(Uuid.getTime());
			record.setcName(request.getParameter("names"));
			record.setcPhoto(request.getParameter("fileupload"));
			record.setcType(request.getParameter("type"));
			record.setcParentlevelid(request.getParameter("Parentlevelid"));
			record.setcAddPeople(request.getSession().getAttribute("aUsername") + "");
			record.setcAddHumanId(request.getSession().getAttribute("aId") + "");
			record.setcStatic("0");
			int insertSelective = categoryManagementBiz.insertSelective(record);
			new CallBack().yes(response, insertSelective, "");
		} catch (Exception e) {
			new CallBack().no(response, "", "");
		}

	}

	/**
	 * @方法名称：updateCateMange
	 * @方法描述：类别管理修改 @创建时间：2019-03-23 13:07:07
	 * @创建人：王俊
	 * @param file
	 * @param request
	 * @param response
	 * @throws IllegalStateException
	 * @throws IOException
	 */
	@RequestMapping("pc/admin/updateCateMange")
	public void updateCateMange(HttpServletRequest request, HttpServletResponse response) {
		try {
			
			CategoryManagement record = new CategoryManagement();
			record.setcPhoto(request.getParameter("fileupload"));
			record.setcName(request.getParameter("names"));
			record.setcId(request.getParameter("cId"));
			CategoryManagementExample example = new CategoryManagementExample();
			
			categoryManagementBiz.updateByExampleSelective(record, example);
			new CallBack().yes(response, "", "");
		} catch (Exception e) {
			new CallBack().no(response, "", "");

		}
	}

	/**
	 * @方法名称：findbigClass
	 * @方法描述：根据type找到类型对于的分类 @创建时间：2019-03-23 13:07:07
	 * @创建人：王俊
	 */
	@RequestMapping("pc/admin/findbigClass")
	public void findbigClass(HttpServletRequest request, HttpServletResponse response) {
		String type = request.getParameter("type");
		CategoryManagement record = new CategoryManagement();
		if (type.equals("1")) {
			record.setcType("1");
		} else if (type.equals("2")) {
			record.setcType("2");
			record.setcParentlevelid(request.getParameter("bigClass"));
		}
		List<CategoryManagement> selectByType = categoryManagementBiz.selectByType(record);
		if (selectByType.size() > 0) {
			new CallBack().yes(response, selectByType, "");
		} else {
			new CallBack().no(response, "", "");
		}
	}

	/**
	 * @方法名称：selectBycParentlevelid
	 * @方法描述：根据父类ID查询子类 @创建时间：2019-03-23 13:07:07
	 * @创建人：王俊
	 */
	@RequestMapping("pc/admin/selectBycParentlevelid")
	public void selectBycParentlevelid(HttpServletRequest request, HttpServletResponse response) {
		response.setContentType("text/xml; charset=UTF-8");
		response.setContentType("text/html;charset=utf-8");
		CategoryManagement record = new CategoryManagement();
		record.setcParentlevelid(request.getParameter("bigClass"));
		List<CategoryManagement> selectByType = categoryManagementBiz.selectBycParentlevelid(record);
		List<Map<String, Object>> list = new ArrayList<Map<String, Object>>();
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("code", 0);
		map.put("msg", "");
		map.put("count", selectByType.size());
		for (int i = 0; i < selectByType.size(); i++) {
			Map<String, Object> mapData = new HashMap<String, Object>();
			mapData.put("id", i + 1);
			mapData.put("getName", selectByType.get(i).getcName());
			mapData.put("CreateTime",
					"<a target=\"_Blank\" href='" + projectPath.url + selectByType.get(i).getcPhoto()
							+ "'><img class='reviewImg' src='" + projectPath.url 
							+ selectByType.get(i).getcPhoto() + "'></a>");
			mapData.put("button", "<a  type='button' class=\"layui-btn layui-btn-xs\" href=\"javascript:table('0','"
					+ selectByType.get(i).getcId() + "','" + selectByType.get(i).getcName()
					+ "')\"'>修改</a><a type='button' class=\"layui-btn layui-btn-danger layui-btn-xs\" lay-event=\"del\"  href=\"javascript:table('1','"
					+ selectByType.get(i).getcId() + "','" + selectByType.get(i).getcName() + "')\">删除</a>");
			list.add(mapData);
		}
		map.put("data", list);
		JSONObject json = JSONObject.fromObject(map);
		new CallBack().textObject(json, response);
	}

	/**
	 * @方法名称：delCid
	 * @方法描述：根据父类ID查询子类 @创建时间：2019-03-23 13:07:07
	 * @创建人：王俊
	 */
	@RequestMapping("pc/admin/delCid")
	public void delCid(HttpServletRequest request, HttpServletResponse response) {
		CategoryManagementExample example = new CategoryManagementExample();
		com.tszs.entity.CategoryManagementExample.Criteria ca = example.createCriteria();
		ca.andCIdEqualTo(request.getParameter("id"));
		int deleteByExample = categoryManagementBiz.deleteByExample(example);
		if (deleteByExample > 0) {
			new CallBack().yes(response, "", "");
		} else {
			new CallBack().no(response, "", "操作失败");
		}
	}

	/**
	 * @方法名称：findBigClass
	 * @方法描述：查询大类 @创建时间：2019-03-23 13:07:07
	 * @创建人：王俊
	 */
	@RequestMapping("txr/findBigClass")
	public void findBigClass(HttpServletRequest request, HttpServletResponse response) {
		CategoryManagementExample example = new CategoryManagementExample();
		com.tszs.entity.CategoryManagementExample.Criteria ca = example.createCriteria();
		ca.andCTypeEqualTo("1");
		ca.andCStaticNotEqualTo("1");
		List<CategoryManagement> selectByExample = categoryManagementBiz.selectByExample(example);
		List<CategoryManagement> list = new ArrayList<>();
		for (int i = 0; i < selectByExample.size(); i++) {
			CategoryManagement categoryManagement = new CategoryManagement();
			categoryManagement.setcId(selectByExample.get(i).getcId());
			categoryManagement.setcName(selectByExample.get(i).getcName());
			String b = "";
			for (int c = 0; c < selectByExample.get(i).getcPhoto().split(",").length; c++) {
				b += projectPath.url  + selectByExample.get(i).getcPhoto().split(",")[c];
			}
			categoryManagement.setcPhoto(b);
			list.add(categoryManagement);
		}
		if (list.size() > 0) {
			new CallBack().yes(response, list, "");
		} else {
			new CallBack().no(response, "", "");
		}
	}


	/**
	 * @方法名称：findCid
	 * @方法描述：根据CID查询 @创建时间：2019-03-23 13:07:07
	 * @创建人：王俊
	 */
	@RequestMapping("txr/findCid")
	public void findCid(HttpServletRequest request, HttpServletResponse response) {
		
	}

}
